/**
 *Copyright(C) 2019 by DefaultCompany
 *All rights reserved.
 *FileName:     LauguageMgr.cs
 *Author:       why
 *Version:      1.0
 *UnityVersion:2018.3.9f1
 *Date:         2019-05-12
 *Description:   
 *History:
*/
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace HHHFramework
{
    public class LanguageTest : Singleton<LanguageTest> {
            
    }

    public interface ILanguageManager : IDisposable
    {
        void Clear();
        void Register<T>(string lauguageID, Action<T> onReceive);
        void SendEvent<T>(string lauguageID);
        void SendAll();
        void UnRegisterEvent<T>(string lauguageID, Action<T> onReceive);
    }
    /// <summary>
    /// �ӿ� ֻ����洢���ֵ���
    /// </summary>
    interface IRegisterations : IDisposable
    {
    }


    /// <summary>
    /// ���ע��
    /// </summary>
    class Registerations<T> : IRegisterations
    {
        /// <summary>
        /// ��Ϊί�б���Ϳ���һ�Զ�ע��
        /// </summary>
        public Action<T> OnReceives = obj => { };

        public void Dispose()
        {
            OnReceives = null;
        }
    }


    //class TypeEventUnregister<T> : IDisposable
    //{
    //    public Action<T> OnReceive;
    //    public IEventManager TypeEventSystem;

    //    public void Dispose()
    //    {
    //        TypeEventSystem.UnRegisterEvent<T>(OnReceive);
    //    }
    //}

    /// <summary>
    /// ֻ�ܼ���UI�ϵ�sprite��text�Ķ�����
    /// </summary>
    public class LanguageMgr : Singleton<LanguageMgr>,ILanguageManager
    {
        /// <summary>
        /// ȫ��ע���¼�
        /// </summary>
        private static readonly ILanguageManager mGlobalEventSystem = new LanguageMgr();

        private Dictionary<string, IRegisterations> dicLauguageCache = new Dictionary<string, IRegisterations>();
        private LanguageData languageData;
        
        public void Ini(EnvironmentMode environmentMode = EnvironmentMode.Release)
        {
            string path ="";
            switch (environmentMode)
            {
                case EnvironmentMode.Developing:
                    {
                      path =  UIConfigDataManager.Get<UISystemConfig>().multipleLanguageDevelop;
                    }
                    break;
                case EnvironmentMode.Test:
                    {
                        path = UIConfigDataManager.Get<UISystemConfig>().multipleLanguageTest;
                    }
                    break;
                case EnvironmentMode.Release:
                    {
                        path = UIConfigDataManager.Get<UISystemConfig>().multipleLanguageRelease;
                    }
                    break;

            }
            languageData =UIConfigDataManager.Get<LanguageData>();
            res = new ResLoad();
            //EventManager.Send<>
        }

        private LanguageMgr() { }

        //public void UnRegist(string lauguageID, UnityAction<string> callBack) {

        //    UnityAction<string> unReg;
        //    if (dicLauguageCache.TryGetValue(lauguageID, out unReg))
        //    {

        //        unReg -= callBack;
        //    }
        //    else
        //    {
        //        LogManager.PrintLog($"���Ϊ��{lauguageID}�����Ա�Ų����� ");     
        //    }
        //}

        //public void send(string lauguageID) {

        //}
        //public void SendAll() {

        //}
        private ResLoad res;
        public void Clear()
        {
            throw new NotImplementedException();
        }

        public void Register<T>(string lauguageID, Action<T> onReceive)
        {

            IRegisterations registerations = null;

            if (dicLauguageCache.TryGetValue(lauguageID, out registerations))
            {
                var reg = registerations as Registerations<T>;
                reg.OnReceives += onReceive;
            }
            else
            {
                var reg = new Registerations<T>();
                reg.OnReceives += onReceive;
                dicLauguageCache.Add(lauguageID, reg);
            }
        }
        public void ChangeLanguage(int languageIndex) {
            LanguageData.Languageindex = languageIndex;
            SendAll();
        }

        public void SendEvent<T>(string lauguageID)
        {

            for (int i = 0; i < languageData.languageModels.Length; i++)
            {
                int index = i;
                if (lauguageID == languageData.languageModels[index].id) {
                    switch (languageData.languageModels[i].assetType)
                    {
                        case "Text":
                            {
                                SendEventText(index);
                            }
                            break;
                        case "Sprite":
                            {
                                SendEventSprite(index);
                            }
                            break;
                    }
                    return;
                }
               
            }
        }
        private void SendEventText(int index)
        {
            string lauguageID = languageData.languageModels[index].id;

            IRegisterations registerations = null;

            if (dicLauguageCache.TryGetValue(lauguageID, out registerations))
            {
                var reg = registerations as Registerations<string>;
                string text = languageData.languageModels[index].languageText[LanguageData.Languageindex];
                reg.OnReceives(text);
            }
        }
        private void SendEventSprite(int index)
        {
            string lauguageID = languageData.languageModels[index].id;

            IRegisterations registerations = null;
            string resPath = languageData.languageModels[index].languageText[LanguageData.Languageindex];
            if (dicLauguageCache.TryGetValue(lauguageID, out registerations))
            {
                var reg = registerations as Registerations<Sprite>;
                Sprite sprite = res.LoadSync<Sprite>(ResKitUtil.GetResourcesPath(resPath));
                reg.OnReceives(sprite);
            }
        }
        public void SendAll()
        {
            for (int i = 0; i < languageData.languageModels.Length; i++)
            {
                int index = i;
                switch (languageData.languageModels[i].assetType)
                {
                    case "Text":
                        {
                            SendEventText(index);
                        } break;
                    case "Sprite":
                        {
                            SendEventSprite(index);
                        } break;
                }
            }
        }
        public void UnRegisterEvent<T>(string lauguageID, Action<T> onReceive)
        {
            IRegisterations registerations = null;

            if (dicLauguageCache.TryGetValue(lauguageID, out registerations))
            {
                var reg = registerations as Registerations<T>;
                reg.OnReceives -= onReceive;
            }
        }

        public void Dispose()
        {
            
        }

       
    }
}